package com.common.platform.base.config.mail;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.apache.commons.mail.MultiPartEmail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.*;
import java.util.Collections;

/**
 * 邮件工具类
 */
@Component
@Slf4j
public class MailUtil {

    @Autowired
    Mail mail;

    /**
     * 发送邮件
     *
     * @param subject 邮件主题
     * @param message 邮件内容
     */
    public void send(String subject, String message) {
        // 发送邮件
        if (mail.isFlag()) {
            HtmlEmail email = new HtmlEmail();
            try {
                // 配置邮件服务器
                email.setHostName(mail.getHost());
                // 编码
                email.setCharset("UTF-8");
                for (String str : mail.getReceivers()) {
                    // 收件人
                    email.addTo(str);
                }
                // 发件人
                email.setFrom(mail.getFrom(), mail.getNickname());
                // 端口
                email.setSmtpPort(mail.getPort());
                // 验证
                email.setAuthentication(mail.getFrom(), mail.getPass());
                // 主题
                email.setSubject(subject);
                // 内容
                email.setMsg(message);
                // 发送
                email.send();
                log.info("{} 发送邮件到 {}", mail.getFrom(), StringUtils.join(mail.getReceivers(), ","));
            } catch (EmailException e) {
                log.error(mail.getFrom() + "发送邮件到" + StringUtils.join(mail.getReceivers(), ",") + "失败", e);
            }
        }
    }


    public void sendWithImage(String subject, String message) {
        // 发送邮件
        if (mail.isFlag()) {
            HtmlEmail email = new HtmlEmail();
            try {
                // 配置邮件服务器
                email.setHostName(mail.getHost());
                // 编码
                email.setCharset("UTF-8");
                for (String str : mail.getReceivers()) {
                    // 收件人
                    email.addTo(str);
                }
                // 发件人
                email.setFrom(mail.getFrom(), mail.getNickname());
                // 端口
                email.setSmtpPort(mail.getPort());
                // 验证
                email.setAuthentication(mail.getFrom(), mail.getPass());
                // 主题
                email.setSubject(subject);
                // 内容
                email.setMsg(message);
                // 添加图片附件
                email.attach(new File("D:\\tianmi的图片\\211d1f5834084bafb7f02bd247ba39e1.jpg"));
                // 发送
                email.send();
                log.info("{} 发送邮件到 {}", mail.getFrom(), StringUtils.join(mail.getReceivers(), ","));


            } catch (EmailException e) {
                log.error(mail.getFrom() + "发送邮件到" + StringUtils.join(mail.getReceivers(), ",") + "失败", e);
            }
        }
    }

    public void send() {
        Mail mail = new Mail();
        mail.setFlag(true);
        mail.setHost("smtp.qq.com");
        //端口
        mail.setPort(25);
        mail.setFrom("2118148193@qq.com");
        mail.setPass("kgrgoqdujxjhhbae");
        mail.setNickname("田秘");
        mail.setReceivers(Collections.singleton("2956714917@qq.com"));
        MailUtil mailUtil = new MailUtil();
        mailUtil.mail = mail;
        mailUtil.sendWithImage("田秘", "1111");
    }

}

